package Utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;

public class ThumbnailUtil {
	public static boolean ThumbnailsCompressPic(String inputFile,
			String outputFile, int size, float quality) {
		File input = new File(inputFile);
		try {
			Thumbnails.Builder<File> fileBuilder = Thumbnails.of(input)
					.scale(1.0).outputQuality(1.0);
			BufferedImage src = fileBuilder.asBufferedImage();
			if (src.getHeight(null) > size || src.getWidth(null) > size) {
				Thumbnails.Builder<File> builder = Thumbnails.of(input);
				builder.size(size, size); // 取最大的尺寸变成size，然后等比缩放
				builder.outputQuality(quality).toFile(outputFile);
			} else {
				Thumbnails.of(input).scale(1.0).outputQuality(quality)
						.toFile(outputFile);
			}
			return true;
		} catch (IOException e) {
			return false;
		}

	}

	public static boolean setImageSize(File imageFile, String outputFile,
			int w, int h) {

		try {

			BufferedImage sourceImg = ImageIO.read(imageFile);
			int iWidth = sourceImg.getWidth();
			int iHeight = sourceImg.getHeight();
			if (iWidth < w) {
				w = iWidth;
			}

			if (iHeight < h) {
				h = iHeight;
			}

			Thumbnails.of(imageFile.getAbsoluteFile()).size(w, h)
					.toFile(outputFile);
			return true;

		} catch (Exception e1) {

			return false;
		}
	}

	public static boolean setImageSize(String srcFile, String outputFile,
			int w, int h) {

		return setImageSize(new File(srcFile), outputFile, w, h);

	}

	public static void main(String[] args) {

		File file = new File("E:\\2018\\马建威\\覆盖度拍照\\8.4照片\\DSC_9468.JPG");

		File target = new File(file.getParent() + "\\small");
		if (!target.exists()) {
			target.mkdirs();
		}

		setImageSize(file.getAbsolutePath(), target.getAbsolutePath() + "\\"
				+ file.getName(), 500, 400);

	}
}